package com.xtz.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.xtz.domain.Category;
import com.xtz.dto.UpdateCategoryDto;
import com.xtz.mapper.AdminCategoryMapper;
import com.xtz.service.AdminCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * date: 2022/5/4 16:49
 *
 * @author xietianze
 */
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {

    @Autowired
    private AdminCategoryMapper adminCategoryMapper;
    @Override
    public Boolean addCategory(Category category) {
        if (category == null){
            return false;
        }
        long id = new Snowflake().nextId();
        category.setId(Long.toString(id));


        category.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        category.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        category.setCreateUser("1");
        category.setUpdateUser("1");

        return adminCategoryMapper.addCategory(category)>0;
    }

    @Override
    public Category selectByname(String name) {
        if (name==null){
            return null;
        }
        Category category = adminCategoryMapper.selectByname(name);
        return category;
    }

    @Override
    public Category updateCategory(Category category) {

        category.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        category.setUpdateUser("1");

        int result = adminCategoryMapper.updateCategory(category);
        if (result==0) {
            return null;
        }
        return category;
    }


    @Override
    public boolean deleteCategory(String id) {
        if (id == null){
            return false;
        }

        return adminCategoryMapper.deleteCategory(id)>0;
    }
    @Override
    public List<Category> selectAll(){
        List<Category> categories = adminCategoryMapper.selectAll();
        return categories;
    }

    @Override
    public List<Category> getCategoryPage(Integer page, Integer pageSize) {
        int offset = (page - 1) * pageSize;

        return adminCategoryMapper.selectAllPage(offset,pageSize);
    }

    @Override
    public Long getCategoryAmount(String name) {
        return adminCategoryMapper.selectCount(name);
    }
}
